package top.birdhk.stream;

import java.io.File;
import java.util.Properties;

public class SecurityUtils {
	private static final String USER_KEYTAB = "sysadmin-realm.keytab";
	private static final String USER_PRINCIPAL = "sysadmin-realm@INDATA.COM";

	public static Properties getSecurityProperties() {
		String config_path = SecurityUtils.class.getResource("/").getFile() + "config";
		File config = new File(config_path);

		Properties props = new Properties();

		if (USER_KEYTAB.length() != 0 && USER_PRINCIPAL.length() != 0
				&& config.exists() && config.isDirectory()) {
			String path = config.getPath().replaceAll("[\\\\/]", "/") + "/";
			System.setProperty("java.security.krb5.conf", path + "krb5.conf");

			StringBuilder jaas = new StringBuilder("com.sun.security.auth.module.Krb5LoginModule required");
			jaas.append(" useKeyTab=true storeKey=true");
			jaas.append(" keyTab=\"" + path + USER_KEYTAB + "\"");
			jaas.append(" principal=\"" + USER_PRINCIPAL + "\";");

			props.put("sasl.jaas.config", jaas.toString());
			props.put("security.protocol", "SASL_PLAINTEXT");
			props.put("sasl.kerberos.service.name", "kafka");
			props.put("sasl.mechanism", "GSSAPI");

		}

		return props;
	}

}
